// pages/electric/index.ts
import request from '../../utils/request'
const app = getApp<IAppOption>()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        globalData: {},
        popup: {},// 提示框
        equipmentNo: "",// 设备号
        info: {},
    },
    // 点击扫码
    handleScan() {
        wx.scanCode({
            success: (res) => {
                let that = this;
                if(res.result) {
                    that.setData({
                      equipmentNo: res.result
                    });
                    that.doSearch(res.result);
                } else {
                    wx.showToast({
                        title: '二维码内容为空',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },

    handleSearch(){
        if(this.data.equipmentNo) {
            this.doSearch(this.data.equipmentNo);
        } else {
            this.setData({
                popup: {
                    idx: Math.random(),
                    show: true,
                    title: '提 示',
                    msg: '请输入设备编号查询！',
                    icon: '../../static/img/no_happy.png'
                },
            })
        }
    },

    doSearch(equipmentNo: string){
        let that = this;
        wx.showLoading({
            title: '查询中...',
        });
        request.get('/applet/equipment/getBraceletBatteryData', { equipmentNo: equipmentNo }).then((res: any) => {
            wx.hideLoading();
            let data = res.data.data;
            if(data) {
                that.setData({
                    info: data
                })
            } else {
                that.setData({
                    popup: {
                        idx: Math.random(),
                        show: true,
                        title: '提 示',
                        msg: '没有查询到数据！',
                        icon: '../../static/img/no_happy.png'
                    },
                })
            }
        })
    },

    goKd(){
        let url = "/pages/login/login";
        if(app.globalData.userInfo.nickName){
            url = "/pages/scankd/scankd?equipmentNo=" + this.data.equipmentNo;
        }

        wx.navigateTo({
            url: url
        });
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            globalData: app.globalData
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})